
export const formatDate = (timestamp = Date.now(), format = "Y-M-D h:m:s") => {
  let date = new Date(timestamp);
  let dateInfo = {
    Y: date.getFullYear(),
    M: date.getMonth() + 1,
    D: date.getDate(),
    h: date.getHours(),
    m: date.getMinutes(),
    s: date.getSeconds(),
  };
  let formatNumber = (n: any) => (n >= 10 ? n : "0" + n);
  let res = format
    .replace("Y", dateInfo.Y as any)
    .replace("M", formatNumber(dateInfo.M))
    .replace("D", formatNumber(dateInfo.D))
    .replace("h", formatNumber(dateInfo.h))
    .replace("m", formatNumber(dateInfo.m))
    .replace("s", formatNumber(dateInfo.s));
  return res;
}

export const isEmpty = (data: any) => {
  return !Array.isArray(data) && ['', 'undefined', 'NaN', 'null'].includes(data + '')
}

export const deepCopy = (obj: { [x: string]: any; constructor: ArrayConstructor; }) => {
  var newobj: any = obj?.constructor === Array ? [] : {};
  if (typeof obj !== 'object') {
    return obj;
  } else {
    for (var i in obj) {
      if (typeof obj[i] === 'object') {
        newobj[i] = deepCopy(obj[i]);
      } else {
        newobj[i] = obj[i];
      }
    }
  }
  return newobj;
}
// 判断的是否是JSON字符串
export const isJSON = (str: string) => {
  if (typeof str == 'string') {
    try {
      var obj = JSON.parse(str);
      // 等于这个条件说明就是JSON字符串 会返回true
      if (typeof obj == 'object' && obj) {
        return true;
      } else {
        // 不是就返回false
        return false;
      }
    } catch (e) {
      return false;
    }
  }
  return false;
}

// 重新组合树数据(根据需要来重组树结构中的属性字段)
import { importComponent } from './router'
export const dealTreeData = (treeData: any[]) => {
  const data: any = treeData.map((item) => ({
    path: item.path,
    component: importComponent(item.path, item.page_type),
    meta: {
      visible: item.visible, // 0 表示显示 1 表示隐藏
      title: item.title,
      keepAlive: true,
      auth: true,
      page_type: item.page_type, // 1 表示一级菜单 2 表示二级菜单 3 表示新增、删除、修改、查询相关接口
    },
    children:
      item.children && item.children.length
        ? dealTreeData(item.children)
        : null,
  }))
  return data
}

export { WindowResizeChange, checkFull } from './dom'